<!-- 生成文字日报 -->
<template>
  <dt-modal-info ref="info" :title="title" :width="700" :height="400" cancelText="取消">
    <div class="create_deily">
      <Row class="label_warp">
        <Col span="24" class="tips">
          <Icon type="md-alert" class="tips_icon" />
          文字日报默认显示“今日”数据，您可通过勾选方式，在日报中添加其他维度的数据
        </Col>
      </Row>
      <div class="flex flex-horizontal-between">
        <div class="check_list">
          <Checkbox
            v-for="(item, index) in topData"
            :key="index"
            @on-change="changeCheck(item, index)"
            v-model="item.checked">
            <span style="margin-left: 10px">{{ item.label }}</span>
          </Checkbox>
        </div>
        <Button type="primary" v-clipboard:copy="copyTxt" v-clipboard:success="onCopy">复制日报</Button>
      </div>
      <div ref="copyDom" class="copy_content">
        <p class="title">{{ buildingName }} {{ currentDate }} 日报</p>
        <!-- 今日小结 -->
        <div v-if="summaryContent">
          <div>
            <p class="label1">=====今日小结=====</p>
            <span>{{ summaryContent }}</span>
          </div>
        </div>

        <!-- pk联代---竞品 -->
        <div v-if="hasShowGeneration && info.length">
          <p class="label1">=====PK数据(联代)=====</p>
          <div v-for="itemPk in info" :key="itemPk.key + 'generation'">
            <span class="label2">{{ itemPk.label }}来人：</span>
            <span v-for="itemG in itemPk.data.generation" :key="itemG.generationId + 'generationZu'">
              {{ itemG.generationName }}:{{ itemG.visitorCount }}组;
            </span>
            <span class="label2">{{ itemPk.label }}成交：</span>
            <span v-for="itemG in itemPk.data.generation" :key="itemG.generationId + 'generationTao'">
              {{ itemG.generationName }}:{{ itemG.orderCount }}套;
            </span>
          </div>
        </div>

        <div v-if="hasShowMatch && info.length">
          <p class="label1">=====PK数据(竞品)=====</p>
          <div v-for="itemPk in info" :key="itemPk.key + 'match'">
            <span class="label2">{{ itemPk.label }}来人：</span>
            <span v-for="itemG in itemPk.data.match" :key="itemG.generationId + 'matchzu'">
              {{ itemG.generationName }}:{{ itemG.visitorCount }}组;
            </span>
            <span class="label2">{{ itemPk.label }}成交：</span>
            <span v-for="itemG in itemPk.data.match" :key="itemG.generationId + 'matchTao'">
              {{ itemG.generationName }}:{{ itemG.orderCount }}套;
            </span>
          </div>
        </div>

        <div v-if="(clueTemplate.hasShow || visitorTemplate.hasShow) && info.length">
          <p class="label1">=====客户统计=====</p>
          <div v-for="item in info" :key="item.key + 'cluevisitor'">
            <div v-if="clueTemplate.hasShow">
              <p>{{ item.label }}线索</p>
              <span v-for="itemClue in clueTemplate.list" :key="itemClue.fieldName">
                {{ itemClue.dailyConfigGenName }}:{{ item.data.customer[itemClue.fieldName] }}组;
              </span>
            </div>
            <div v-if="visitorTemplate.hasShow">
              <p>{{ item.label }}来人</p>
              <span v-for="itemVisitor in visitorTemplate.list" :key="itemVisitor.fieldName">
                <span v-if="itemVisitor.fieldName != 'clComerCount'">
                  {{ itemVisitor.dailyConfigGenName }}:{{ item.data.customer[itemVisitor.fieldName] }}组;
                </span>
                <span v-else-if="itemVisitor.hasLock">
                  (其中{{ item.data.customer[itemVisitor.fieldName] }}组为线索转到访客户);
                </span>
              </span>
            </div>
          </div>
        </div>

        <div v-if="channelTemplate.hasShow">
          <div v-for="item in info" :key="item.key + 'channel'">
            <p class="label1" v-if="item.data.channelList.length">=====渠道统计（{{ item.label }}）=====</p>
            <div
              v-for="itemChannelData in item.data.channelList"
              :key="itemChannelData.channelId + 'qudaochannelData'">
              <p>{{ itemChannelData.channelName }}：</p>
              <span v-for="itemChannel in channelTemplate.list" :key="itemChannel.fieldName">
                {{ itemChannel.dailyConfigShortName }}:{{ itemChannelData[itemChannel.fieldName] }}组;
              </span>
            </div>
          </div>
        </div>

        <div v-if="salesTemplate.hasShow">
          <div v-for="item in info" :key="item.key + 'sales'">
            <p class="label1">=====销售统计（{{ item.label }}）=====</p>
            <p v-for="itemSales in salesTemplate.list" :key="itemSales.fieldName">
              {{ itemSales.dailyConfigShortName }}:
              <span v-if="itemSales.fieldName !== 'cumulateBuyback' && itemSales.fieldName !== 'buyback'">
                {{ item.data.sales[itemSales.fieldName + 'Quantity'] || '0' }}套
              </span>
              <span v-if="itemSales.fieldName !== 'cumulateBuyback' && itemSales.fieldName !== 'buyback'">
                (
              </span>
              {{ item.data.sales[itemSales.fieldName + 'Amount'] || '0' }}元
              <span v-if="itemSales.fieldName !== 'cumulateBuyback' && itemSales.fieldName !== 'buyback'">
                )
              </span>
            </p>
          </div>
        </div>
      </div>
    </div>
    <dt-loading :loading="loading" />
  </dt-modal-info>
</template>

<script>
  import homeMgr from '@/biz/buildingHome/home'
  import moment from 'moment'
  import dailyApi from '@/biz/buildingHome/daily'
  export default {
    data() {
      return {
        copyTxt: '',
        loading: false,
        info: [],
        topData: [
          {
            key: 'dayList',
            label: '今日',
            sort: 1,
            checked: true
          },
          {
            key: 'weekList',
            label: '本周',
            sort: 2,
            checked: false
          },
          {
            key: 'monthList',
            label: '本月',
            sort: 3,
            checked: false
          },
          {
            key: 'yearList',
            label: '本年',
            sort: 4,
            checked: false
          },
          {
            key: 'allList',
            label: '累计',
            sort: 5,
            checked: false
          }
        ],
        title: '生成文字日报',
        checkTime: {},

        buildingName: this.$dt.buildInfo().housesName,
        currentDate: moment(new Date()).format('YYYY-MM-DD'),
        summaryContent: '',
        clueTemplate: {
          hasShow: false,
          list: []
        },
        visitorTemplate: {
          hasShow: false,
          list: []
        },
        channelTemplate: {
          hasShow: false,
          list: []
        },
        salesTemplate: {
          hasShow: false,
          list: []
        },
        hasShowGeneration: false,
        hasShowMatch: false,
        clueMaxSort: '',
        monthTargets: {},
        monthNum: ''
      }
    },
    methods: {
      updateTask() {
        let params = {
          taskId: 35,
          buildingId: this.$dt.buildInfo().housesId,
          date: this.currentDate,
          taskStatus: 2
        }
        dailyApi.updateTask(params).then((res) => {
          console.log('完成')
        })
      },
      getMaxSort(arr) {
        this.clueMaxSort = arr.reduce((max, item) => {
          if (item.sort < 4) {
            return Math.max(max, item.sort)
          }
          return max
        }, -1)
      },
      show(data) {
        this.currentDate = data.currentDate
        this.info = []
        this.topData.map((item) => {
          if (item.key != 'dayList') {
            item.checked = false
          } else {
            item.checked = true
          }
        })
        this.checkTime = {}
        this.changeCheck(this.topData[0], '0')
        this.getSummaryData()
        this.$refs.info.show()
        this.getClueTemplate(data)
        this.getVisitorTemplate(data)
        this.getChannelTemplate(data)
        this.getSalesTemplate(data)
      },
      //选择时间
      changeCheck(item, index) {
        let idx = Number(index) + 1
        if (item.checked) {
          if (item.key == 'allList') {
            this.checkTime.endTime = ''
            this.checkTime.startTime = ''
          } else {
            this.checkTime = dailyApi.resovleTime(idx, this.currentDate)
          }
          this.getData(item, index)
        } else {
          this.info = this.info.filter((el) => {
            return item.sort != el.sort
          })
          this.getMaxSort(this.info)
        }
      },
      onCopy() {
        let copyData = this.$refs.copyDom.innerText
        this.copyTxt = copyData
        this.$copyText(copyData).then((message) => {
          this.$Message.success('内容已复制到剪切板！')
        })
        this.updateTask()
        dailyApi.logGenerate({ actionType: 'copy_daily' })
      },
      getData(obj) {
        this.loading = true
        // 获取完数据push到info里
        let params = {
          buildingId: this.$dt.buildInfo().housesId,
          startTime: this.checkTime.startTime,
          endTime: this.checkTime.endTime
        }
        dailyApi.dailyPaperMerge(params).then((res) => {
          let generation = []
          let match = []
          if (res.channelList.length > 5) {
            res.channelList = res.channelList.splice(0, 5)
          } else {
            res.channelList = res.channelList.splice(0, res.channelList.length)
          }
          res.pkDataList.length &&
            res.pkDataList.forEach((item) => {
              if (item.projectType == 0) {
                generation.unshift(item)
                match.unshift(item)
              }
              if (item.projectType == 1) {
                generation.push(item)
              } else if (item.projectType == 2) {
                match.push(item)
              }
            })
          let item = {
            ...res,
            generation,
            match
          }
          obj.data = item
          this.info.push({ ...obj })
          this.info = this.info.sort((a, b) => a.sort - b.sort)
          this.hasShowGeneration = this.info.some((itemInfo) => {
            return itemInfo.data.generation.length > 0
          })
          this.hasShowMatch = this.info.some((itemInfo) => {
            return itemInfo.data.match.length > 0
          })
          this.getMaxSort(this.info)
          this.loading = false
        })
      },
      //获取今日小结数据
      getSummaryData() {
        let params = {
          date: this.currentDate,
          buildingId: this.$dt.buildInfo().housesId
        }
        dailyApi.findDailySection(params).then((res) => {
          this.summaryContent = res.content ? res.content : ''
        })
      },

      //根据模板设置 获取线索统计模板
      getClueTemplate(data) {
        this.clueTemplate.hasShow = data.templateAllList[0].hasShow
        this.clueTemplate.list = data.templateAllList[0].childModelConfig.filter((item) => {
          return item.hasLock
        })
      },

      //根据模板设置 获取来人统计模板
      getVisitorTemplate(data) {
        this.visitorTemplate.hasShow = data.templateAllList[1].hasShow
        this.visitorTemplate.list = data.templateAllList[1].childModelConfig.filter((item) => {
          return item.hasLock
        })
      },

      //根据模板设置 获取渠道统计模板
      getChannelTemplate(data) {
        this.channelTemplate.hasShow = data.templateAllList[2].hasShow
        this.channelTemplate.list = data.templateAllList[2].childModelConfig.filter((item) => {
          return item.hasLock
        })
      },

      //根据模板设置 获取销售统计模板
      getSalesTemplate(data) {
        this.salesTemplate.hasShow = data.templateAllList[3].hasShow
        let tmpList = data.templateAllList[3].childModelConfig.filter((item) => {
          return item.hasLock
        })
        this.salesTemplate.list = tmpList.sort((a, b) => a.manualSerialNo - b.manualSerialNo).slice(0, 5)
      }
    },
    mounted() {
      this.copyTxt = `123123`
    }
  }
</script>
<style lang="scss" scoped>
  .create_deily {
    .label_warp {
      text-align: center;
      margin-bottom: 15px;
      background-color: rgba(254, 252, 236, 1);
      width: auto;
      line-height: 32px;
      padding: 0 10px;

      .tips {
        line-height: 32px;
        font-size: 14px;

        .tips_icon {
          font-size: 20px;
          color: #0f42a7;
          vertical-align: sub;
        }
      }
    }

    .flex {
      border-bottom: 1px solid #ccc;
    }

    .check_list {
      width: 60%;
      display: flex;
      justify-content: space-between;
      line-height: 32px;
      padding-bottom: 15px;
    }

    .copy_content {
      padding: 15px;
      font-size: 14px;
      max-height: 500px;
      min-height: 250px;
      overflow-y: auto;

      .title {
        font-size: 16px;
        font-weight: 600;
        padding-bottom: 15px;
      }

      .label1 {
        font-size: 15px;
        font-weight: 600;
        padding-top: 10px;
      }

      .label2 {
        display: block;
      }
    }
  }
</style>
